import $ from 'jQuery';
import URI from 'URI'
require('../less/common.less');
require('../less/login.less');
$(function () {
    // 验证码
    var option = {
        mode: 0, // captcha
        captchaDom: '.form-verify-img',
        url: 'login/captcha'
    };
    $('.forgetPwd-form-hook').IOTCaptcha(option);
    
    var $sendCode = $('.send-code-hook');
    // 发送验证码方法
    var _fnSendCode = function (obj) {
        obj.off('click').on('click', function () {
            var mobilePhone = $('input[name=mobile]').val();
            var param = {
                mobile: mobilePhone
            };
            var regex = /^1[3|4|5|7|8]\d{9}$/;
            if (regex.test(mobilePhone)) {
                IOT.getServerData(URI.LOGIN.RETRIEVE_PASSWORD.SEND, param, function (ret) {
                    if (ret && ret.code === 200) {
                        IOT.tips('验证码已经发送！', 'success', 1000, function () {
                            $sendCode.off('click');
                            $sendCode.removeClass('send-code');
                            $sendCode.addClass('disable-click');
                            _setTime(obj);
                        });
                    } else {
                        IOT.tips(ret.message || '请求超时', 'error', 1000, null);
                    }
                });
            } else {
                IOT.tips('手机号码输入有误！', 'error', 1000, null);
            }

        });
    };
    _fnSendCode($sendCode);
    
    // 等待60秒
    var countdown = 60;
    function _setTime (obj) {
        if (countdown === 0) {
            obj.removeClass("disable-click");
            obj.addClass("send-code");
            obj.html("发送验证码");
            countdown = 60;
            _fnSendCode(obj);
            _fnSendEmail(obj);
            return;
        } else {
            obj.removeClass("send-code");
            obj.addClass("disable-click");
            obj.html("重新发送(" + countdown + ")");
            countdown--;
        }
        setTimeout(function () {
            _setTime(obj)
        }, 1000)
    }
        
    // 确定提交 验证手机号码和验证码是否匹配
    $('.forgetPwd-form-hook').validate({
        debug: true,
        rules: {
            forget_mobile: {
                phoneCN: true,
                required: true,
            },
            smsCode: {
                required: true,
            },
            forget_captcha: {
                required: true,
                maxlength: 10
            }
        },
        messages: {
            forget_mobile: {
                required: '请输入手机号'
            },
            smsCode: {
                required:'请输入手机验证码',
            },
            forget_captcha: {
                required: '请输入验证码！'
            }
        },
        submitHandler: function (form) {
            let params = {
                mobile: $('#forget_mobile').val(),
                smsCode: $('#forget_smsCode').val(),
                captcha: $('.form-yzm').val()
            };
            params.type = '2';
            IOT.showOverlay('验证中，请稍等...');
            IOT.getServerData(URI.LOGIN.RETRIEVE_PASSWORD.CHECK, params, function (ret) {
                IOT.hideOverlay();
                if (ret && ret.code === 200) {
                    if (ret.data) {
                        IOT.tips('验证成功！', 'success', 1000, function () {
                            IOT.setSessionStore(URI.LOGIN.RETRIEVE_PASSWORD.REINSTALL.PAGE, params.mobile);
                            IOT.redirect2URL('/login');
                        });
                    } else {
                        IOT.tips('验证未通过', 'error', 1000, null);
                    }
                } else {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                    if (ret.code === 401) {
                        $('#forget_scode').trigger('click');
                    }
                }
            });
            return false;
        }
    });
    
    $('.btn-cancel').off('click').on('click', function () {
        IOT.redirect2URL('/login');
    });
    
    $(document).keyup(function (e) {
        if (e.keyCode === 13) {
            // $('.btn-login').trigger('click');
            // $form.submit();
        }
    });
});
